package sdk.pay.utils;


import android.content.Context;
import android.os.Environment;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public final class PayExceptionUtil implements Thread.UncaughtExceptionHandler {
    private Context mContext;
    private Thread.UncaughtExceptionHandler mDefaultHandler;

    @Override
    public void uncaughtException(Thread thread, Throwable throwable) {
        if (!handleException(throwable) && null != mDefaultHandler) {
            mDefaultHandler.uncaughtException(thread, throwable);
        } else {
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }

    private static class InstanceHolder {
        static PayExceptionUtil handler = new PayExceptionUtil();
    }

    public static PayExceptionUtil getInstance() {
        return InstanceHolder.handler;
    }

    private PayExceptionUtil() {

    }

    public void init(Context context) {
        mContext = context;
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    private void saveFile(Throwable throwable) {
        String str = Log.getStackTraceString(throwable);
        saveFile(str);
    }

    public void saveExceptionFile(String exceptionMsg) {
        exceptionMsg = exceptionMsg + "\n";
        saveFile(exceptionMsg);
    }

    public void saveFile(String message) {
        String fileName = "exception_" + PayDateFormatUtil.getDate() + ".log";
        boolean ret = PayFileUtil.hasSDCardPermission(mContext);
        if (ret) {
            String storagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + mContext.getPackageName();
            boolean canWrite = Environment.getExternalStorageDirectory().canWrite();
            PayLogUtil.logBase("canWrite = " + canWrite);
            File pathFile = new File(storagePath);
            if (!pathFile.exists()) {
                pathFile.mkdirs();
            }
            synchronized (this) {
                File file1 = new File(pathFile, fileName);
                FileWriter fileWriter = null;
                try {
                    fileWriter = new FileWriter(file1, true);
                    fileWriter.write(message);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    PayAutoCloseUtil.quietClose(fileWriter);
                }
            }
        }
    }

    private boolean handleException(Throwable throwable) {
        if (null == throwable) {
            return false;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                Toast.makeText(mContext, "program quit caused by exception ", Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        }).start();
        saveFile(throwable);
        return true;
    }

}
